import axios from 'axios';
import store from '../store/index'

// 设置默认请求地址
console.log(process.env)
 //判断是否为生产环境
if (process.env.NODE_ENV == 'development') {
  axios.defaults.baseURL = "http://127.0.0.1/";
}else{
  axios.defaults.baseURL = window.location.origin;
}
//设置请求超时时间
axios.defaults.timeout = 10000;
//设置POST请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';


/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: {
        ...params,
        // jwt_token: store.state.jwt_token,
        // source_type: 'wechat'
      }
    }).then(res => {
      resolve(res.data);
    }).catch(err => {
      reject(err.data)
    })
  });
}

/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, {
        ...params,
        // jwt_token: store.state.jwt_token,
        // source_type: 'wechat'
      })
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        reject(err.data)
      })
  });
}
